package com.ssm.equip.server.controller.indoor;

import com.github.pagehelper.PageInfo;
import com.jee.ssm.common.support.Const;
import com.jee.ssm.common.utils.UUIDFactory;
import com.jee.ssm.common.web.json.Tip;
import com.jee.ssm.common.web.param.ParamMap;
import com.ssm.equip.dao.model.Indoor;
import com.ssm.equip.rpc.api.indoor.IndoorService;
import com.ssm.upms.dao.model.Account;
import com.ssm.upms.log.AdminControllerLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
* 室内机管理 Controller
* @author GaoXiang
* @version 1.0
*/
@Controller
@RequestMapping("/indoor")
public class IndoorController  {


    /**
     * 进入室内机添加页面 携带一个生成的id --> longId
     * @param model 返回的实体容器
     * @return 添加页面
     */
    @RequestMapping(value="/add")
    @RequiresPermissions("indoor:add")
    public String add(ModelMap model, HttpSession session){
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        model.put("longId", UUIDFactory.getStringId());
        model.put("type", account.getType());
        return "manager/indoor/add";
    }


    /**
     * 进入室内机编辑页面
     * @param model 返回室内机的容器
     * @param id 室内机id
     * @return 编辑页面
     */
    @RequestMapping(value="/edit")
    @RequiresPermissions("indoor:edit")
    public String edit(ModelMap model, @RequestParam String id, HttpSession session) {
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        model.put("data",indoorService.selectById(id));
        model.put("type",account.getType());
        return "manager/indoor/edit";
    }


    /**
     * 室内机添加
     * @param indoor 带id的室内机对象
     * @return 成功状态
     */
    @RequestMapping(value="/save")
    @ResponseBody
    @RequiresPermissions("indoor:add")
    @AdminControllerLog(description = "添加室内机" )
    public Tip save(Indoor indoor)  {
        try {
            if(indoorService.countNumber(indoor)>0){
                return new Tip(1,"该房间已经绑定室内机！");
            }
            indoor.setCreateTime(new Date());
            indoorService.insert(indoor);
            return new Tip();
        } catch (Exception e) {
            e.printStackTrace();
            return new Tip(1,"添加失败！");
        }

    }


    /**
     * 根据 id 修改室内机
     * @param indoor 带id的室内机对象
     * @return 成功状态
     */
    @RequestMapping(value="/update")
    @ResponseBody
    @RequiresPermissions("indoor:edit")
    @AdminControllerLog(description = "修改室内机" )
    public Tip update(Indoor indoor) {

        try {
            if(!indoorService.selectById(indoor.getId()).getRoomId().equals(indoor.getRoomId())) {
                if (indoorService.countNumber(indoor) > 0) {
                    return new Tip(1, "该房间已经绑定室内机!");
                }
            }
            indoorService.updateById(indoor);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"修改失败！");
        }

    }


    /**
     * 根据 id 删除室内机
     * @param id 室内机id
     * @return 成功状态
     */
    @RequestMapping(value="/delete")
    @ResponseBody
    @RequiresPermissions("indoor:delete")
    @AdminControllerLog(description = "删除室内机" )
    public Tip delete(@RequestParam String id) {

        try {
            indoorService.deleteById(id);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"删除失败！");
        }

    }


    /**
     * 根据 id 列表批量删除
     * @param ids 室内机id List
     * @return 成功状态
     */
    @RequestMapping(value="/deleteByIds")
    @ResponseBody
    @RequiresPermissions("indoor:delete")
    @AdminControllerLog(description = "批量删除室内机" )
    public Tip deleteByIds(@RequestParam("ids") List<String> ids) {

        try {
            indoorService.deleteByIds(ids);
            return new Tip();
        } catch (Exception e) {
            //e.printStackTrace();
            return new Tip(1,"批量删除失败！");
        }

    }


    /**
     * 根据 id 查找室内机
     * @param id 室内机id
     * @return 室内机对象 json
     */
    @RequestMapping(value="/findJson")
    @ResponseBody
    @RequiresPermissions("indoor:list")
    public Indoor find(@RequestParam String id) {

        return indoorService.selectById(id);
    }


    /**
     * 获取室内机列表 获取全部 不分页
     * @param request 请求参数
     * @return 室内机列表页面
     */
    @RequestMapping(value="/all")
    @RequiresPermissions("indoor:list")
    public String all(HttpServletRequest request, ModelMap modelMap) {

        return list(request,modelMap,1,0);
    }


    /**
     * 获取室内机列表 分页
     * @param request 请求参数
     * @param page 第几页
     * @param size 每页大小
     * @return 室内机列表页面
     */
    @RequestMapping(value="/list")
    @RequiresPermissions("indoor:list")
    public String list(HttpServletRequest request, ModelMap modelMap, Integer page, Integer size) {
        HttpSession session=request.getSession();
        Account account= (Account) session.getAttribute(Const.ACCOUNT);
        ParamMap paramMap=new ParamMap(request);

        if (account.getType()==2){
            paramMap.put("estateId",account.getInfoId());
        }else if (account.getType()==21){
            paramMap.put("estateId",account.getInfoId().split(",")[0]);
            paramMap.put("residenceId",account.getInfoId().split(",")[1]);
        }/*else if(account.getType()==5){
            List<PoliceResidence> policeResidenceList= policeResidenceService.findByPoliceId(account.getInfoId());
            List<String> policeIdList=new ArrayList<String>();
            if(policeResidenceList.size()>0){
                for(PoliceResidence pr:policeResidenceList){
                    policeIdList.add(pr.getResidenceId());
                }
            }else {
                policeIdList.add("0");
            }
            paramMap.put("policeIdList", policeIdList);
        }*/
       /* List<Residence> residences = residenceService.listWithoutPage(paramMap);//搜索下拉框选择小区

        PageInfo<Indoor> pageInfo = indoorService.list(paramMap,page,size);
        if (pageInfo.getList().size()>0){
            for (Indoor in:pageInfo.getList()) {
                in.setResidence(residenceService.selectById(in.getResidenceId()));
                WebSocketSession wsession = PhoneWSUtil.get("room_" + in.getId());
                if(wsession != null) {
                    in.setOnline("在线");
                    Object version = PhoneWSUtil.statusMap.get("room_" + in.getId() + "_version");
                    if (version != null) {
                        in.setVersion(version.toString());
                    }
                    Object lastDate = PhoneWSUtil.statusMap.get("room_" + in.getId() + "_lastDate");
                    if (lastDate != null) {
                        in.setEndTime((Date) lastDate);
                    }
                }else{
                    in.setOnline("离线");
                    in.setVersion("未获取");
                }
            }
        }
        modelMap.put("pageInfo",pageInfo);
        modelMap.put("residences",residences);//搜索下拉框选择小区*/
        modelMap.put("type",account.getType());
        modelMap.putAll(paramMap);
        return "manager/indoor/list";
    }


    //---------------------------- property -------------------------------

    @Resource
    private IndoorService indoorService;
/*    @Resource
    private ResidenceService residenceService;
    @Resource
    private PoliceResidenceService policeResidenceService;
    @Resource
    private BuildingService buildingService;
    @Resource
    private UnitService unitService;
    @Resource
    private RoomService roomService;*/

}
